package com.gqr.springcloud.biz;

import com.gqr.springcloud.topics.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class Controller {

    @Resource
    private MyTopic producer;

    @Resource
    private GroupTopic groupTopicProducer;

    @Resource
    private DelayedTopic delayedTopicProducer;

    @Resource
    private ErrorTopic errorTopicProducer;

    @Resource
    private RequeueTopic requeueTopicProducer;
    @Resource
    private DlqTopic dlqTopicProducer;

    @Resource
    private FallbackTopic fallbackTopicProducer;

    @PostMapping("send")
    public void sendMessage(@RequestParam("body") String body) {
        producer.output().send(MessageBuilder.withPayload(body).build());
    }

    @PostMapping("sendToGroup")
    public void sendMessageToGroup(@RequestParam("body") String body) {
        groupTopicProducer.output().send(MessageBuilder.withPayload(body).build());
    }

    @PostMapping("sendDM")
    public void sendDelayedMessage(@RequestParam("body") String body,
                                   @RequestParam("seconds") Integer seconds
    ) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);

        log.info("ready to send delayed message");
        delayedTopicProducer.output().send(
                MessageBuilder.withPayload(msg)
                        .setHeader("x-delay", seconds * 1000)
                        .build());
    }

    // 异常重试(单机版)
    @PostMapping("sendError")
    public void sendErrorMessage(@RequestParam("body") String body) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);

        errorTopicProducer.output().send(MessageBuilder.withPayload(msg).build());
    }

    // 异常重试(联机版-重新入列)
    @PostMapping("requeue")
    public void sendErrorMessageToMQ(@RequestParam("body") String body) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);

        requeueTopicProducer.output().send(MessageBuilder.withPayload(msg).build());
    }


    // 死信队列
    @PostMapping("dlq")
    public void sendMessageToDlq(@RequestParam("body") String body) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);

        dlqTopicProducer.output().send(MessageBuilder.withPayload(msg).build());
    }

    // fallback + 升级版本
    @PostMapping("fallback")
    public void sendMessageToFallback(
            @RequestParam("body") String body,
            @RequestParam(value = "version",defaultValue = "1.0") String version
    ) {
        MessageBean msg = new MessageBean();
        msg.setPayload(body);
        // 下单接口
        // 客户端app版本不同
        //  placeOrderV1
        // placeOrderV2

        // queue 1
        // queue 2

        fallbackTopicProducer.output().send(
                MessageBuilder.withPayload(msg)
                        .setHeader("version",version)
                        .build());
    }

}
